package com.foreverwin.jzymes.production.grind.service.impl;

import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.grind.domain.GrindLog;
import com.foreverwin.jzymes.production.grind.mapper.GrindLogMapper;
import com.foreverwin.jzymes.production.grind.service.IGrindLogAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class GrindLogAdviceServiceImpl implements IGrindLogAdviceService {

    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    GrindLogMapper grindLogMapper;


    /**
     * 创建研磨产出日志
     *
     * @param grindLog
     */
    @Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void createGrinLog( GrindLog grindLog ){

        /**检查字段*/
        checkFieldsForGrindLog( grindLog );

        grindLog.setCreateUserBo( CommonMethods.getUserBo() );
        grindLog.setCreatedDateTime( new Date() );
        grindLog.setHandle( BOHandleUtil.buildGrindLogHandle( grindLog.getSite(), grindLog.getItemBo(), grindLog.getCreatedDateTime() ) );

        String itemBo = commonService.findSfcItemByItem( grindLog.getSite(), grindLog.getItemBo() );

        /**查找工作中的工令，更新已下达数量、良品入库数量、实际开始日期字段后保存*/
        commonService.updateGrindSfcForPass( grindLog.getSite(), itemBo, grindLog.getOkQty(), grindLog.getResrce(), grindLog.getCreatedDateTime() );

        /**保存研磨产出记录*/
        grindLogMapper.insertGrindLog( grindLog );
    }


    /**
     * 检查研磨产出相关字段
     *
     * @param grindLog
     */
    public void checkFieldsForGrindLog( GrindLog grindLog ){
        /**检查站点*/
        siteAdviceService.existSite( grindLog.getSite() );
        /**检查设备*/
        resourceAdviceService.existResource( grindLog.getSite(), grindLog.getResrce() );
        /**检查机种*/
        /*String itemGroup = itemGroupAdviceService.findItemGroupBySite( grindLog.getSite() );
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotExist", new String[]{ itemGroup } ) );
        grindLog.setMachineType( itemGroup );*/
        String itemGroup = commonService.findItemGroupByItem( grindLog.getSite(), grindLog.getItemBo() );
        grindLog.setMachineType( itemGroup );
        /**检查物料*/
        itemAdviceService.existItem( grindLog.getItemBo() );
        /**检查良品数不能为空*/
        Assert.isTrue( grindLog.getOkQty() != null && grindLog.getOkQty() > 0, I18nUtil.getI18nText("production.grind.okQtyNotValid", new Object[]{ grindLog.getOkQty() } ) );
    }
}
